package de.dhbw.asa.model;

import java.sql.ResultSet;

/**
 * Class for the status of a order
 * @author aar
 */
public class Status extends Database
{

	public int iStatusNr;
	public String sStatusName;
	
	/*
	 * this method loads the Status
	 */
	public boolean load(int iStatusNr)
	{
		boolean bReturn = false;String sStatusNr = String.valueOf(iStatusNr);
		StringBuffer sbQuery = new StringBuffer();
		sbQuery.append("Select * from Status where StatusNr = '");
		sbQuery.append(sStatusNr);
		sbQuery.append("';");
		try
		{
			if (this.createConnection())
			{
				ResultSet jResult = this.executeQuery(sbQuery.toString());
				if (jResult.next())
				{
					this.iStatusNr = iStatusNr;
					this.sStatusName = jResult.getString("statusName");
				}
				bReturn = true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return bReturn;
	}
	
	
	/*
	 * this method saves changes of a status
	 */
	public String saveChanges()
	{
		String sReturn = "";
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Insert the Order into the Database
			sbQuery.append("update status set statusName= '");
			sbQuery.append(this.sStatusName);
			sbQuery.append("' where statusNr =");
			sbQuery.append(this.iStatusNr);
			sbQuery.append(";");
			this.execute(sbQuery.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	
	/*
	 * this method saves a new status in the database
	 */
	public String saveNewStatus(String sStatusName)
	{
		String sReturn = "";
		try
		{
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			// Finding out the last OrderNr of our orders
			sbQuery.append("Select statusNr from Status;");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			int iLastStatusNr = 0;
			while (jResult.next())
			{
				iLastStatusNr = jResult.getInt("StatusNr");
			}
			this.iStatusNr = iLastStatusNr+1;
			StringBuffer sbQuery2 = new StringBuffer();
			// Insert the customer into the Database
			sbQuery2.append("insert into status (statusNr, statusName) values (");
			sbQuery2.append(this.iStatusNr);
			sbQuery2.append(",'");
			sbQuery2.append(this.sStatusName);
			sbQuery2.append("');");
			this.execute(sbQuery2.toString());
			this.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			sReturn = e.getMessage();
		}
		return sReturn;
	}
	

	public int getiStatusNr() 
	{
		return iStatusNr;
	}

	public void setiStatusNr(int iStatusNr) 
	{
		this.iStatusNr = iStatusNr;
	}

	public String getsStatusName() 
	{
		return sStatusName;
	}

	public void setsStatusName(String sStatusName) 
	{
		this.sStatusName = sStatusName;
	}
	
}
